//
//  ChooseCityViewController.m
//  WeatherOFChangsha
//
//  Created by zkingsoft on 15/12/16.
//  Copyright (c) 2015年 zkingsoft. All rights reserved.
//

#import "ChooseCityViewController.h"

@interface ChooseCityViewController ()<UIPickerViewDataSource, UIPickerViewDelegate, UIAlertViewDelegate>{
    NSMutableArray *_cityArray;
    
    NSInteger _selectRow;
    NSInteger _selectRow2;
}

@end

@implementation ChooseCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _cityArray = [[NSMutableArray alloc] init];
    _selectRow = 0;
    _selectRow2 = 0;
    
    self.chooseCityV = [[ChooseCityView alloc] init];
    self.view = self.chooseCityV;
    self.chooseCityV.cityPickerView.dataSource = self;
    self.chooseCityV.cityPickerView.delegate = self;
    
    // 获取已有完整的路径
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ProvincesAndCities" ofType:@"plist"];
    NSMutableArray *cityArr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    if (cityArr) {
        [_cityArray setArray:cityArr];
    }else{
        _cityArray = [[NSMutableArray alloc] initWithObjects:nil, nil];
    }
    
    // 给按钮添加方法
    [self.chooseCityV.backButton addTarget:self action:@selector(BackButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.chooseCityV.sureCityButton addTarget:self action:@selector(SureButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - pickerViewDele
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return _cityArray.count;
    }else{
        return [[[_cityArray objectAtIndex:_selectRow] objectForKey:@"Cities"] count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return [[_cityArray objectAtIndex:row] objectForKey:@"State"];
    }else{
        return [[[[_cityArray objectAtIndex:_selectRow] objectForKey:@"Cities"] objectAtIndex:row] objectForKey:@"city"];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        _selectRow = row;
        [self.chooseCityV.cityPickerView reloadComponent:1];
    }else{
        _selectRow2 = row;
        [self.chooseCityV.cityPickerView reloadComponent:1];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 60;
}

#pragma mark - ButtonClicked
- (void)BackButtonClicked:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)SureButtonClicked:(UIButton *)sender{
    [self performSelector:@selector(chooseCityString) withObject:nil afterDelay:0.618];
}

- (void)chooseCityString{
    NSInteger count1 = [self.chooseCityV.cityPickerView selectedRowInComponent:0];
    NSInteger count2 = [self.chooseCityV.cityPickerView selectedRowInComponent:1];
    
    NSString *choiceCity = [[[[_cityArray objectAtIndex:count1] objectForKey:@"Cities"] objectAtIndex:count2] objectForKey:@"city"];
    self.cityName = choiceCity;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您选择的城市是:%@", choiceCity] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [[NSUserDefaults standardUserDefaults] setObject:self.cityName forKey:@"cityName"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"====");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
